<?php

namespace Home\Controller;

use Think\Controller;
use Home\Biz\AdminBiz;
use Home\Biz\PromoterBiz;
use Home\Biz\OrderBiz;
use Home\Biz\SkuBiz;
use Home\Biz\CommonBiz;
/**
 * 页面ajax请求服务处理Controller
 * 
 * @author Simon
 *        
 */
class AjaxServiceController extends Controller {
	/**
	 * Ajax服务中转类
	 */
	public function center($moudle, $util) {
		
		CommonBiz::saveOperation(cookie('login_user_id'), 1, C('AdminConfig.system_domain'), $moudle, $util,CommonBiz::getParamsStr());

        if(AdminBiz::verifyUser() == false){
            $this->responseFail(5, '非法用户！');
        }

		switch (strtolower ( $moudle )) {
			case 'common' :
				$this->commonService ( $util );
				break;
			case 'admin' :
				$this->adminService ( $util );
				break;
			case 'order' :
				$this->orderService ( $util );
				break;
			case 'promoter' :
				$this->promoterService ( $util );
				break;
			case 'sku' :
				$this->skuService ( $util );
				break;
			default :
				$this->responseFail ( 5, '无法找到服务模块' );
		}
	}
	
	/**
	 * admin 模块
	 * 
	 * @param string $util
	 *        	方法名
	 */
	public function adminService($util) {
		switch (strtolower ( $util )) {
			case 'test' :
				$param = I('param');
				echo $param;
				return;
			case 'login' :
				// 用户登陆
				$user_id = I ( 'param.user_id' );
				$password = I ( 'param.password' );
				
				$error_msg = AdminBiz::login ( $user_id, $password );
				
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				return;
    		case 'logout':
    				//用户登陆
    			$user_id = I('param.user_id');
    			$error_msg = AdminBiz::logout($user_id);
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    			$this->responseFail(5, $error_msg);
    			}
    		return;
			default :
				$this->responseFail ( 5, '无法找到服务方法' );
		}
	}
	public function promoterService($util) {
		switch ($util) {
			case 'searchPromoterList' :
				$promoter_en = array (
						'promoter_id' => I ( 'promoter_id' ),
						'cellphone' => I ( 'cellphone' ),
						'promoter_type' => I ( 'promoter_type' ),
						'promoter_name' => I ( 'promoter_name' ),
						'page_index' => I ( 'page_index' ),
						'page_size' => I ( 'page_size' ) 
				);
				
				$return_en = PromoterBiz::searchPromoterList ( $promoter_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'searchAmountList' :
				$param_en = array (
						'promoter_id' => I ( 'promoter_id' ),
						'page_index' => I ( 'page_index' ),
						'page_size' => I ( 'page_size' ) 
				);
				
				$return_en = PromoterBiz::searchAmountList ( $param_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'searchWithdrawAmountList' :
				$param_en = array (
						'promoter_id'   => I ( 'promoter_id' ),
						'page_index'    => I ( 'page_index' ),
						'page_size'     => I ( 'page_size' )
				);
				
				$return_en = PromoterBiz::searchWithdrawAmountList ( $param_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'searchWithdrawList' :
				$param_en = array (
					'promoter_id'   => I ( 'promoter_id' ),
					'promoter_name' => I ( 'promoter_name' ),
                    'status'        => I ( 'status' ),
					'page_index'    => I ( 'page_index' ),
					'page_size'     => I ( 'page_size' )
				);
			
				$return_en = PromoterBiz::searchWithdrawList ( $param_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'resetPwd' :
				// 重置密码
				$promoter_id = I ( 'promoter_id' );
				
				$error_msg = PromoterBiz::resetPwd ( $promoter_id );
				
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
			case 'updateAmount' :
				$param_en = array (
						'promoter_id'	=> I ( 'promoter_id' ),
						'amount'		=> I ( 'amount' ),
						'amount_kind'	=> I ( 'amount_kind' ),
						'amount_type'	=> I ( 'amount_type' ),
						'remark'		=> I ( 'remark' ),
				);
				$error_msg = PromoterBiz::updateAmount ( $param_en, 'whaleyFZ.admin AjaxService promoterService.updateAmount' );
				
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
			case 'updatePromoterStatus':
				$promoter_id= I ( 'promoter_id' );
				$reject_reason= I ( 'reject_reason' );
				$status		= I ( 'status' );
				$error_msg = PromoterBiz::updatePromoterStatus($promoter_id,$reject_reason, $status);
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
			default :
				$this->responseFail ( 5, '无法找到服务方法' );
				break;
		}
	}
	public function orderService($util) {
		switch ($util) {
			case 'searchOrderList' :
				$params_en = array (
						'promoter_id' 		=> I ( 'promoter_id' ),
						'promoter_name' 	=> I ( 'promoter_name' ),
						'order_id' 			=> I ( 'order_id' ),
						'order_status' 		=> I ( 'order_status' ),
						'payment_status' 	=> I ( 'payment_status' ),
                        'order_type' 	    => I ( 'order_type' ),
						'begin_order_date'	=> urldecode(I('begin_order_date')),
						'end_order_date'	=> urldecode(I('end_order_date')),
						'page_index' 		=> I ( 'page_index' ),
						'page_size' 		=> I ( 'page_size' ) 
				);
				
				$return_en = OrderBiz::search ( $params_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'cancelOrder':
				$order_id = I('order_id');
				$error_msg = OrderBiz::cancelOrder($order_id, 'whaleyFZ AjaxService orderService.cancelOrder');
				if(empty($error_msg)){
					$this->responseSuccess();
				}
				else{
					$this->responseFail(5, $error_msg);
				}
				break;
				break;
			default :
				$this->responseFail ( 5, '无法找到服务方法' );
				break;
		}
	}
	public function responseSuccess($result) {
		// 0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
		$return_json = array (
				'IsSuccess' => 1,
				'ErrorMsg' => '',
				'ErrorCode' => 0,
				'Result' => $result 
		);
		$this->ajaxReturn ( $return_json );
	}
	public function responseFail($error_code, $error_msg) {
		// 0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
		$return_json = array (
				'IsSuccess' => 0,
				'ErrorMsg' => $error_msg,
				'ErrorCode' => $error_code,
				'Result' => null 
		);
		$this->ajaxReturn ( $return_json );
	}
	
	public function skuService($util) {
		switch ($util) {
			case 'searchSkuList' :
				$sku_en = array (
				'style_sn' => I ( 'style_sn' ),
				'style_name' => I ( 'style_name' ),
				'is_buy_fz' => I ( 'is_buy_fz' ),
				'page_index' => I ( 'page_index' ),
				'page_size' => I ( 'page_size' )
				);
				$return_en = SkuBiz::searchSkuList ( $sku_en );
				$this->responseSuccess ( $return_en );
				break;
			case 'updateSku':
				$sku_id = I ( 'sku_id' );
				$sku_sn = I ( 'sku_sn' );
				$sku_name = I ( 'sku_name' );
				$taobao_sku_id = I ( 'taobao_sku_id' );
				$taobao_sku_properties_name = I ( 'taobao_sku_properties_name' );
				$style_sn = I ( 'style_sn' );
				$style_name = I ( 'style_name' );
				$promotion_text = I ( 'promotion_text' );
				$taobao_style_id  = I ( 'taobao_style_id' );
				$taobao_style_name = I ( 'taobao_style_name' );
				$taobao_category_id = I ( 'taobao_category_id' );
				$sku_dimension_id1 = I ( 'sku_dimension_id1' );
				$sku_dimension_name1 = I ( 'sku_dimension_name1' );
				$sku_dimension_value1 = I ( 'sku_dimension_value1' );
				$sku_dimension_id2 = I ( 'sku_dimension_id2' );
				$sku_dimension_name2 = I ( 'sku_dimension_name2' );
				$sku_dimension_value2  = I ( 'sku_dimension_value2' );
				$market_price = I ( 'market_price' );
				$sale_price = I ( 'sale_price' );
				$person_commission_amount = I ( 'person_commission_amount' );
				$dealer_commission_amount = I ( 'dealer_commission_amount' );
				$image = I ( 'image' );
				$image_list = I ( 'image_list' );
				$material_no = I ( 'material_no' );
				$material_desc = I ( 'material_desc' );
				$code  = I ( 'code' );
				$haier_material_no = I ( 'haier_material_no' );
				$material_length = I ( 'material_length' );
				$material_width = I ( 'material_width' );
				$material_height = I ( 'material_height' );
				$material_volume = I ( 'material_volume' );
				$material_weight = I ( 'material_weight' );
				$is_buy_fz = I ( 'is_buy_fz' );
				$status = I ( 'status' );
				 $error_msg = SkuBiz::updateSku($sku_id ,$sku_sn,$sku_name,$taobao_sku_id,$taobao_sku_properties_name ,$style_sn,
												$style_name,$promotion_text,$taobao_style_id ,$taobao_style_name,$taobao_category_id ,
												$sku_dimension_id1 ,$sku_dimension_name1 ,$sku_dimension_value1 ,$sku_dimension_id2,
												$sku_dimension_name2,$sku_dimension_value2 ,$market_price ,$sale_price,$person_commission_amount,
												$dealer_commission_amount,$image,$image_list ,$material_no ,$material_desc,$code ,
												$haier_material_no,$material_length ,$material_width,$material_height ,$material_volume ,
												$material_weight ,$is_buy_fz,$status );
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
		    case 'addSku':
				$sku_sn = I ( 'sku_sn' );
				$sku_name = I ( 'sku_name' );
				$taobao_sku_id = I ( 'taobao_sku_id' );
				$taobao_sku_properties_name = I ( 'taobao_sku_properties_name' );
				$style_sn = I ( 'style_sn' );
				$style_name = I ( 'style_name' );
				$promotion_text = I ( 'promotion_text' );
				$taobao_style_id  = I ( 'taobao_style_id' );
				$taobao_style_name = I ( 'taobao_style_name' );
				$taobao_category_id = I ( 'taobao_category_id' );
				$sku_dimension_id1 = I ( 'sku_dimension_id1' );
				$sku_dimension_name1 = I ( 'sku_dimension_name1' );
				$sku_dimension_value1 = I ( 'sku_dimension_value1' );
				$sku_dimension_id2 = I ( 'sku_dimension_id2' );
				$sku_dimension_name2 = I ( 'sku_dimension_name2' );
				$sku_dimension_value2  = I ( 'sku_dimension_value2' );
				$market_price = I ( 'market_price' );
				$sale_price = I ( 'sale_price' );
				$person_commission_amount = I ( 'person_commission_amount' );
				$dealer_commission_amount = I ( 'dealer_commission_amount' );
				$image = I ( 'image' );
				$image_list = I ( 'image_list' );
				$material_no = I ( 'material_no' );
				$material_desc = I ( 'material_desc' );
				$code  = I ( 'code' );
				$haier_material_no = I ( 'haier_material_no' );
				$material_length = I ( 'material_length' );
				$material_width = I ( 'material_width' );
				$material_height = I ( 'material_height' );
				$material_volume = I ( 'material_volume' );
				$material_weight = I ( 'material_weight' );
				$is_buy_fz = I ( 'is_buy_fz' );
				$status = I ( 'status' );
				 $error_msg = SkuBiz::addSku($sku_sn,$sku_name,$taobao_sku_id,$taobao_sku_properties_name ,$style_sn,
												$style_name,$promotion_text,$taobao_style_id ,$taobao_style_name,$taobao_category_id ,
												$sku_dimension_id1 ,$sku_dimension_name1 ,$sku_dimension_value1 ,$sku_dimension_id2,
												$sku_dimension_name2,$sku_dimension_value2 ,$market_price ,$sale_price,$person_commission_amount,
												$dealer_commission_amount,$image,$image_list ,$material_no ,$material_desc,$code ,
												$haier_material_no,$material_length ,$material_width,$material_height ,$material_volume ,
												$material_weight ,$is_buy_fz,$status );
				if (empty ( $error_msg )) {
					$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
			default :
				$this->responseFail ( 5, '无法找到服务方法' );
				break;
		}
	}
}